package com.chentf.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author chentengfei
 * @desc ...
 * @date 2021-09-12 11:23:29
 */
@RestController

public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired //服务注册与发现客户端对象
    private DiscoveryClient discoveryClient;

    @Autowired //负载均衡客户端对象
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("user")
    public String invokeDemo(){
        log.info("user demo....");
        //1.调用订单服务,服务地址uri:http://localhost:9999/order,必须get方式,接收返回值String类型
        //tring result = restTemplate.getForObject("http://localhost:9999/order", String.class);

        //2.使用ribbon组件 + RestTemplate实现负载均衡策略 1.DiscoveryClient 2.LoadBalanceClient 3.@LoadBalance
        //List<ServiceInstance> serviceInstances = discoveryClient.getInstances("ORDERS");
        //serviceInstances.forEach(serviceInstance -> {
        //    log.info("服务主机:{},服务端口:{},服务地址:{}",serviceInstance.getHost(),serviceInstance.getPort(),serviceInstance.getUri());
        //});
        //String result = restTemplate.getForObject(serviceInstances.get(0).getUri() + "/order", String.class);

        //3.使用进行服务调用
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("ORDERS"); //默认是轮询策略
        //log.info("服务地址:{} 服务主机:{} 服务端口:{}",serviceInstance.getUri(),serviceInstance.getHost(),serviceInstance.getPort());
        //String result = restTemplate.getForObject(serviceInstance.getUri() + "/order", String.class);

        //4.使用@LoadBalanced注解,作用:可以让对象具有ribbon负载均衡特性
        String result = restTemplate.getForObject("http://ORDERS/order", String.class);

        return "ok!!!"+result;

    }

    //自定义随机策略
    public String randomHost(){
        List<String> hosts = new ArrayList<>();
        hosts.add("localhost:9999");
        hosts.add("localhost:9990");
        //生成随机数 只能在0-hosts.size()
        int i = new Random().nextInt(hosts.size());
        return hosts.get(i);
    }


}
